//
//  CLScrollView.m
//  MasonryTest
//
//  Created by Charles on 16/9/23.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLScrollView.h"

@interface CLScrollView()

@property (strong, nonatomic) UIScrollView* scrollView;

@end

@implementation CLScrollView

- (instancetype)init{
    if (self = [super init]) {
        [self makeView];
    }
    return self;
}

- (void)makeView{
    self.scrollView = UIScrollView.new;
    self.scrollView.backgroundColor = [UIColor grayColor];
    [self addSubview:self.scrollView];
    
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self generateContent];
    
}


- (void)generateContent{
    UIView * contentView = UIView.new;
    [self.scrollView addSubview:contentView];
    
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    UIView * lastView = nil;
    CGFloat height = 25;
    for (int i = 0; i< 10; i++) {
        UIView * view = UIView.new;
        view.backgroundColor = [self randomColor];
        [contentView addSubview:view];
        
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
        [view addGestureRecognizer:singleTap];

        [view makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView?lastView.bottom:@0);
            make.left.equalTo(@0);
            make.width.equalTo(contentView.width);
            make.height.equalTo(@(height));
        }];
        height += 25;
        lastView = view;
    }
    
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.bottom);
    }];
}

- (UIColor *)randomColor {
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

- (void)singleTap:(UITapGestureRecognizer*)sender {
    [sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O
    [self.scrollView scrollRectToVisible:sender.view.frame animated:YES];
};

@end
